<template>
  <div class='xtx-goods-page'>
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem v-if="goods" :to="`/category/${goods.categories[1].id}`">{{ goods.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem v-if="goods" :to="`/category/sub/${goods.categories[0].id}`">{{ goods.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem v-if="goods" to="/">{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <!-- 商品图片 -->
          <GoodsImages v-if="goods" :images="goods.mainPictures"></GoodsImages>
          <!-- 销量人气 商品评价 收藏人气 品牌信息 -->
          <GoodsSales></GoodsSales>
        </div>
        <div class="spec">
          <!-- 商品名称信息 -->
          <GoodsName :goods="goods"></GoodsName>
          <!-- sku组件 -->
          <GoodsSku v-if="goods"  :goods="goods" @change="changeSku"></GoodsSku>
          <!-- 数量组件 -->
          <XtxNumbox label="数量" v-if="goods" v-model="num" :max="goods.inventory"></XtxNumbox>
          <!-- 加入购物车按钮 -->
          <XtxButton @click="inserCart" type="primary" style="margin-top: 20px;">加入购物</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs></GoodsTabs>
          <!-- 注意事项 -->
        <GoodsWarn></GoodsWarn>
        </div>
        <!-- 24热榜+周热榜 -->
        <div class="goods-aside">
          <GoodsHot :type="1"></GoodsHot>
          <GoodsHot :type="2"></GoodsHot>
          <GoodsHot :type="3"></GoodsHot>
        </div>
        
      </div>
    </div>
  </div>
</template>

<script>
import { nextTick, provide, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findGoods } from '@/api/product'
import GoodsRelevant from './components/goods-relevan'
import GoodsImages from '@/views/goods/components/goods-images'
import GoodsSales from '@/views/goods/components/goods-sales.vue'
import GoodsName from '@/views/goods/components/goods-name.vue'
import GoodsSku from '@/views/goods/components/goods-sku.vue'
import GoodsTabs from '@/views/goods/components/goods-tabs.vue'
import GoodsHot from '@/views/goods/components/goods-hot.vue'
import GoodsWarn from '@/views/goods/components/goods-warn.vue'
import { useStore } from 'vuex'
import Message from '@/components/library/Message'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsRelevant,
    GoodsImages,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsTabs,
    GoodsHot,
    GoodsWarn
  },
  setup() {
    //记录sku
    let currSku = ref(null)
    // 获取商品详情
    const useGoods = () => {
      const goods = ref(null)
      const route = useRoute()
      //监听地址栏ID的变化更新页面
      watch(() => route.params.id, (newValue) => {
        if (newValue && `/product/${newValue}` === route.path) {
          findGoods(newValue).then(data => {
            goods.value = null
            nextTick(() => {
              goods.value = data.result
            })

          })
        }
      }, { immediate: true })

      return goods
    }


    //SKU组件传出的数据
    const changeSku = (sku) => {
      // 修改商品的原价现价库存信息
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
      //记录选择后的sku 可能有数据可能没有数据
      currSku.value = sku
    }
    const goods = useGoods()
    //计数组件数量
    const num = ref(1)
    
    //把goods数据提供给后代组件
    provide('goods', goods)

    //加入购物车
    const store = useStore()
    const inserCart = () => {
      if(currSku.value && currSku.value.skuId) {
        const {skuId, specsText:attrsText,inventory:stock} = currSku.value
        const { id,name,price,mainPictures } =goods.value
        store.dispatch('cart/insertCart',{
          id,
          skuId, 
          name: name, 
          attrsText, 
          picture: mainPictures[0], 
          nowPrice:price , 
          price,
          stock,
          selected:true,
          isEffective:true,
          count:num.value
        }).then(() => {
          Message({type:'success',text:'已经添加到购物车'})
        })
      } else {
        Message({type:'warn',text:'请选择完整的规格'})
      }
      
    }

    return {
      goods,
      num,
      changeSku,
      inserCart
    }
  }
}

</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

.goods-tabs {
  min-height: 600px;
  background: #fff;
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>